﻿using Infrastructures;
using IRepository;
using Models;
using Spring.Data.Common;
using Spring.Data.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryMySQL
{
    public class UserInfoRepository : IUserInfoRepository
    {
        private string baseUserInfo = "select UserNo,UserName,NickName,Password,Phone,QQ,WeChart,Role,UserType,Email,Sex,Status,CreateTime,UpdateTime,AgentPassword,HeadImage from UserInfo";
        public UserInfo Create(UserInfo obj)
        {
            string strIns = @"insert into UserInfo(UserName,NickName,Password,Phone,QQ,WeChart,Role,UserType,Email,Sex,Status,CreateTime,UpdateTime,AgentPassword,HeadImage) values(@UserName,@NickName,@Password,@Phone,@QQ,@WeChart,@Role,@UserType,@Email,@Sex,@Status,@CreateTime,@UpdateTime,@AgentPassword,@HeadImage);SELECT LAST_INSERT_ID() as id";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("UserNo", DbType.Int32).Value = obj.UserNo;
            p.Add("UserName", DbType.String).Value = obj.UserName;
            p.Add("NickName", DbType.String).Value = obj.NickName;
            p.Add("Password", DbType.String).Value = obj.Password;
            p.Add("Phone", DbType.String).Value = obj.Phone;
            p.Add("QQ", DbType.String).Value = obj.QQ;
            p.Add("WeChart", DbType.String).Value = obj.WeChart;
            p.Add("Role", DbType.String).Value = obj.Role;
            p.Add("UserType", DbType.String).Value = obj.UserType;
            p.Add("Email", DbType.String).Value = obj.Email;
            p.Add("Sex", DbType.String).Value = obj.Sex;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("CreateTime", DbType.DateTime).Value = DateTime.Now;
            p.Add("UpdateTime", DbType.DateTime).Value = obj.UpdateTime;
            p.Add("AgentPassword", DbType.String).Value = obj.AgentPassword;
            p.Add("HeadImage", DbType.String).Value = obj.HeadImage;

            int num = Int32.Parse(DBHelper.TemplateMySQL.ExecuteScalar(CommandType.Text, strIns, p).ToString());
            obj.UserNo = num;
            return obj;
        }
        public void Update(UserInfo obj)
        {
            string strUpdate = @"update UserInfo set UserName=@UserName,NickName=@NickName,Password=@Password,Phone=@Phone,QQ=@QQ,WeChart=@WeChart,Role=@Role,UserType=@UserType,Email=@Email,Sex=@Sex,Status=@Status,CreateTime=@CreateTime,UpdateTime=@UpdateTime,AgentPassword=@AgentPassword,HeadImage=@HeadImage where UserNo=@UserNo";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("UserNo", DbType.Int32).Value = obj.UserNo;
            p.Add("UserName", DbType.String).Value = obj.UserName;
            p.Add("NickName", DbType.String).Value = obj.NickName;
            p.Add("Password", DbType.String).Value = obj.Password;
            p.Add("Phone", DbType.String).Value = obj.Phone;
            p.Add("QQ", DbType.String).Value = obj.QQ;
            p.Add("WeChart", DbType.String).Value = obj.WeChart;
            p.Add("Role", DbType.String).Value = obj.Role;
            p.Add("UserType", DbType.String).Value = obj.UserType;
            p.Add("Email", DbType.String).Value = obj.Email;
            p.Add("Sex", DbType.String).Value = obj.Sex;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("CreateTime", DbType.DateTime).Value = obj.CreateTime;
            p.Add("UpdateTime", DbType.DateTime).Value = DateTime.Now;
            p.Add("AgentPassword", DbType.String).Value = obj.AgentPassword;
            p.Add("HeadImage", DbType.String).Value = obj.HeadImage;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strUpdate, p);
        }
        public UserInfo Get(string userno)
        {
            string strSel = baseUserInfo + " where UserNo=@UserNo";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("UserNo", DbType.Int32).Value = userno.ToInt();


            IList<UserInfo> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<UserInfo>(MapUserInfo), p);
            return lst.Count == 0 ? null : lst[0];
        }
        public IList<UserInfo> List()
        {
            string strSel = baseUserInfo;

            IList<UserInfo> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<UserInfo>(MapUserInfo));

            return lst;
        }

        public IList<IDictionary<string, string>> ListAgent()
        {
            string strSel = "select NickName, CreateTime from UserInfo where Role=@Role order by CreateTime desc limit 8";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("Role", DbType.String).Value = APPRole.Agent.ToString();

            IList<IDictionary<string, string>> lst = new List<IDictionary<string, string>>();

            DBHelper.TemplateMySQL.QueryWithRowCallbackDelegate(CommandType.Text, strSel, (dr) =>
            {
                IDictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("NickName", dr.GetStringIfNotNull(0));
                dic.Add("CreateTime", dr.GetDateIfNotNull(1).ToString());

                lst.Add(dic);
            }, p);

            return lst;
        }

        private UserInfo MapUserInfo(IDataReader dr, int count)
        {
            UserInfo obj = new UserInfo();
            obj.UserNo = dr.GetInt32(0);
            obj.UserName = dr.IsDBNull(1) ? String.Empty : dr.GetString(1);
            obj.NickName = dr.IsDBNull(2) ? String.Empty : dr.GetString(2);
            obj.Password = dr.IsDBNull(3) ? String.Empty : dr.GetString(3);
            obj.Phone = dr.IsDBNull(4) ? String.Empty : dr.GetString(4);
            obj.QQ = dr.IsDBNull(5) ? String.Empty : dr.GetString(5);
            obj.WeChart = dr.IsDBNull(6) ? String.Empty : dr.GetString(6);
            obj.Role = (APPRole)Enum.Parse(typeof(APPRole), dr.GetString(7), true);
            obj.UserType = dr.IsDBNull(8) ? String.Empty : dr.GetString(8);
            obj.Email = dr.IsDBNull(9) ? String.Empty : dr.GetString(9);
            obj.Sex = (Sex)Enum.Parse(typeof(Sex), dr.GetString(10), true);
            obj.Status = (CommonStatus)Enum.Parse(typeof(CommonStatus), dr.GetString(11), true);
            obj.CreateTime = dr.GetDateTime(12);
            obj.UpdateTime = dr.GetDateTime(13);
            obj.AgentPassword = dr.IsDBNull(14) ? String.Empty : dr.GetString(14);
            obj.HeadImage = dr.IsDBNull(15) ? String.Empty : dr.GetString(15);

            return obj;
        }


        public Models.UserInfo Login(string loginValue, string password)
        {
            string strSql = "select UserNo,UserName,NickName,Password,Phone,QQ,WeChart,Role,UserType,Email,Sex,Status,CreateTime,UpdateTime,AgentPassword,HeadImage from UserInfo where (UserName=@loginValue or Email=@loginValue) and password=@password";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("loginValue", DbType.String).Value = loginValue;
            p.Add("password", DbType.String).Value = password;

            IList<UserInfo> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                 new RowMapperDelegate<UserInfo>(MapUserInfo), p);

            return lst.Count == 0 ? null : lst[0];
        }

        public bool IsUserExist(string userName)
        {
            string strSql = "SELECT count(1) FROM UserInfo where UserName=@User_Name";
            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("User_Name", DbType.String).Value = userName;

            int num = Int32.Parse(DBHelper.TemplateMySQL.ExecuteScalar(CommandType.Text, strSql, p).ToString());

            return num > 0 ? true : false;
        }

        public void UpdatePassword(string userName, string password)
        {
            string sql = "Update UserInfo Set password=@password where username=@user_name";
            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("User_Name", DbType.String).Value = userName;
            p.Add("password", DbType.String).Value = password;
            DBHelper.TemplateMySQL.ExecuteScalar(CommandType.Text, sql, p);
        }

        public Models.UserInfo GetUser(string userName)
        {
            throw new NotImplementedException();
        }
    }
}
